﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class ThuTienPhat : DevComponents.DotNetBar.Controls.SlidePanel
    {
        DocGia _docGia = new DocGia();
        SqlDocGia _sqlDocGia = new SqlDocGia();

        PhieuThuTienPhat _phieuThu = new PhieuThuTienPhat();
        SqlPhieuThuTienPhat _sqlPhieuThu = new SqlPhieuThuTienPhat();

        public ThuTienPhat()
        {
            InitializeComponent();
            this.SlideOutButtonVisible = false;            
        }

        public void UpdateSize()
        {
            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
            grbThongTin.Location = new Point(
                this.Size.Width / 2 - grbThongTin.Size.Width / 2,
                this.Size.Height / 2 - grbThongTin.Size.Height / 2);
        }

        public void ShowSlide()
        {            
            ResetField();
            this.IsOpen = true;
            UpdateSize();
        }

        private void ResetField()
        {
            txtMaDG.Text = null;
            txtTenDG.Text = null;
            txtTienNo.Text = "0";
            txtTienThu.Text = "0";
            txtConlai.Text = "0";
            dtimeNgay.Value = DateTime.Today;
            panel_Result.Text = "";            
        }

        public void SetValue(int maDG)
        {            
            txtMaDG.Text = maDG.ToString();
            getDocGia();
        }

        private void getDocGia()
        {
            _docGia.MaDocGia = int.Parse(txtMaDG.Text);            
            DataTable table = _sqlDocGia.GetById(_docGia);
            if (table.Rows.Count > 0)
            {
                txtTenDG.Text = (string)table.Rows[0]["HoTen"];
                txtTienNo.Text = table.Rows[0]["TongNo"].ToString();
            }
            else
                MessageBox.Show("Mã độc giả không tồn tại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private string Nhap()
        {
            _phieuThu.MaDocGia = int.Parse(txtMaDG.Text);
            _phieuThu.NgayThu = dtimeNgay.Text;
            _phieuThu.SoTienThu = long.Parse(txtTienThu.Text);            
            string result = _sqlPhieuThu.Insert(_phieuThu);
            if (result == "Success")
                return "Đã nhập thành công";
            else
                return result;
            
        }

        private void txtMaDG_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaDG.Text))
            {
                getDocGia();
            }
        }

        private void txtTienThu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }

        private void txtTienThu_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTienThu.Text))
                txtTienThu.Text = "0";
            txtConlai.Text = (float.Parse(txtTienNo.Text) - float.Parse(txtTienThu.Text)).ToString();
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }

        private void butNhap_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaDG.Text))
            {
                panel_Result.Text = Nhap();                
            }
            else
                panel_Result.Text = "Hãy nhập mã độc giả";
        }        

        private void txtMaDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (!string.IsNullOrEmpty(txtMaDG.Text))
                    getDocGia();
        }

        private void butNhapLai_Click(object sender, EventArgs e)
        {
            ResetField();
            txtMaDG.Focus();
        }        
    }
}
